package com.bw.a2209clubshop.ui.mall.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.a2209clubshop.R
import com.bw.a2209clubshop.base.BaseToolbarFragment
import com.bw.a2209clubshop.databinding.FragmentMallBinding
import com.bw.a2209clubshop.ui.mall.adapter.MallHomeMultiAdapter
import com.bw.a2209clubshop.ui.mall.adapter.MallHomeTypeAdapter
import com.bw.a2209clubshop.ui.mall.model.MallHomeLiveData
import com.bw.a2209clubshop.ui.mall.model.MallModel
import com.bw.a2209clubshop.ui.mall.model.MallRowTypes
import com.bw.a2209clubshop.ui.mall.viewmodel.MallViewModel
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MallFragment @Inject constructor() : BaseToolbarFragment<MallViewModel, FragmentMallBinding>() {
    override fun initToolbar(toolbar: Toolbar) {
        // 隐藏左侧返回按钮
        toolbar.navigationIcon = null
    }

    override val mLayoutResId: Int
        get() = R.layout.fragment_mall

    private val mViewModel: MallViewModel by viewModels()
    var mallAdapter:MallHomeMultiAdapter=MallHomeMultiAdapter()
    var mallCateAdapter:MallHomeTypeAdapter=MallHomeTypeAdapter()
    private val mallList: ArrayList<MallModel> = ArrayList()
////    private val homeLiveData by lazy { MallHomeLiveData() }

    override fun initView() {
        super.initView()

        mBinding.viewEmptyUi.container.visibility = View.GONE
        mViewModel.getMallData()
        mViewModel.getMallBanner()
        mViewModel.getMallCate()
//        mBinding.liveData = homeLiveData
        initAdapter()
    }
    fun initAdapter(){
        val spanCount = 2
        val layoutManager=mBinding.rvMallHome.layoutManager as GridLayoutManager
        layoutManager.spanCount = spanCount
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (mallAdapter.getItemViewType(position) == MallRowTypes.GOODS) {
                    1
                } else {
                    spanCount
                }
            }
        }
        mBinding.rvMallHome.layoutManager=layoutManager
        mBinding.rvMallHome.adapter=mallAdapter
        mallAdapter.setOnItemClickListener { adapter, view, position ->
//            val item = adapter.getItem(position) as MallModel
            val intent = Intent(requireContext(), GoodsDetailActivity::class.java)
//            intent.putExtra("goodsId", item.goodsList?.get(0)?.id)
            startActivity(intent)
        }

        mBinding.rvMotorSeries.layoutManager=LinearLayoutManager(requireContext(),LinearLayoutManager.HORIZONTAL,false)
        mBinding.rvMotorSeries.adapter=mallCateAdapter
    }


    @SuppressLint("NotifyDataSetChanged")
    override fun initData() {
        super.initData()
        mallAdapter.data.clear()
        mViewModel.goodsResultLiveData.observe(this){
            mallList.clear()
            it.forEach { item->
                val mallModel=MallModel(MallRowTypes.TITLE,titleList = listOf(item), goodsList =null, bannerList = null)
                mallList.add(mallModel)
                item.mallGoodsInfoPageResponseList.forEach { child->
                    val mallModel=MallModel(MallRowTypes.GOODS,titleList = null, goodsList = listOf(child), bannerList = null)
                    mallList.add(mallModel)
                }
            }

//            Log.d("mallAdapter", "initData2: ---"+mallList.toString())
//            mallAdapter.data.addAll(mallList)
//            mallAdapter.notifyDataSetChanged()
//            it.forEach {item->
//
//
//                Log.d("mallAdapter", "initData2: ---"+mallList.toString())
//            }

            mallAdapter.data.addAll(mallList)
            mallAdapter.notifyDataSetChanged()

        }
        mViewModel.bannerList.observe(this){
            mallList.clear()
            val mallModel=MallModel(MallRowTypes.BANNER,titleList = null, goodsList = null, bannerList = it)
            mallList.add(mallModel)
            Log.d("mallAdapter", "initData2: ---"+mallList.toString())
            mallAdapter.data.addAll(mallList)
            mallAdapter.notifyDataSetChanged()
        }
        mViewModel.cateListLiveData.observe(this){
            mallCateAdapter.data.clear()
            mallCateAdapter.data.addAll(it)
            mallCateAdapter.notifyDataSetChanged()
        }


    }


}